﻿// <copyright file="PhysicsEngine.cs" company="Community Combat Simulator">
//    Community Combat Simulator: a free combat flight simulator.
//    Copyright (C) 2008 Aaron Schinkowitch
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see http://www.gnu.org/licenses/.
// </copyright>
namespace CCSim.Physics
{
    using System;
    using System.Collections.Generic;
    using CCSim.Util;
    using CCSim.Vecmath;
    
    /// <summary>
    /// The PhysicsEngine performs the work of calculating the physical movements of the objects whose motions
    /// are being simulated.
    /// </summary>
    public class PhysicsEngine
    {
        /// <summary>
        /// The force aggregator performs the work of calculating the forces and torques acting on the objects.
        /// </summary>
        private ForceAggregator forceAggregator;
        
        /// <summary>
        /// The rigid bodies whose motion are being simulated.
        /// </summary>
        private List<RigidBody> rigidBodies;
        
        /// <summary>
        /// Initializes a new instance of the PhysicsEngine class with zero rigid bodies.
        /// </summary>
        public PhysicsEngine()
        {
            this.ForceAggregator = new ForceAggregator();
            this.RigidBodies = new List<RigidBody>();
        }

        /// <summary>
        /// Gets or sets the force aggregator used by the simulation.
        /// </summary>
        internal ForceAggregator ForceAggregator 
        {
            get { return this.forceAggregator; }
            set { this.forceAggregator = value; }
        }
        
        /// <summary>
        /// Gets or sets the rigid bodies in the simulation.
        /// </summary>
        internal List<RigidBody> RigidBodies 
        {
            get { return this.rigidBodies; }
            set { this.rigidBodies = value; }
        }
        
        /// <summary>
        /// Adds a rigid body to the simulation.
        /// </summary>
        /// <param name="rigidBody">The rigid body to add. It must be initialized.</param>
        public void Add(RigidBody rigidBody)
        {
            if (!rigidBody.IsInitialized())
            {
                throw new ArgumentException("Rigid body must be initialized.", "rigidBody");
            }
            
            this.rigidBodies.Add(rigidBody);
        }

        /// <summary>
        /// Advances the simulation to the given time.
        /// </summary>
        /// <param name="time">The time to which to advance the simulation.</param>
        public void Advance(double time)
        {
            foreach (RigidBody rigidBody in this.RigidBodies)
            {
                ForceAggregator.ForceAndTorque forceAndTorque 
                    = this.ForceAggregator.SumForces(rigidBody);
                rigidBody.ComputeNextState(time, forceAndTorque.Force, forceAndTorque.Torque);
            }
        }
    }
}
